import axios from "axios";
import cookie from 'react-cookies'

const request = axios.create({    //创建axios实例，在这里可以设置请求的默认配置
  baseURL: 'http://liufusong.top:8888'
})


// 请求拦截器
const token = cookie.load('usertoken')
if (token) {
  request.interceptors.request.use((config) => {
    config.headers.Authorization = 'Bearer ' + token
    return config
  })
}


request.interceptors.response.use((res) => {
  // console.log(res);
  const { status, data, msg, message } = res
  // console.log(data, status);
  if (status >= 200 && status <= 400) {
    return data
  } else {
    console.log(msg || message);
  }
}, (err) => {
  if (err.response && err.response === 401) {
    console.log(err);
  } else {
    console.log(err);
  }
}) // 响应拦截器

export default request